home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Tool Chest / Networking / Network Watch (DMZ) v1.3 / dMZLists.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-16  |  14.1 KB  |  636 lines  |  [TEXT/MPS ]

  1. /*
  2. #-------------------------------------------------------------------------------------------
  3. #
  4. #    Program:    < DMZ 1.3 >
  5. #    File:        < dmzLists.c >
  6. #    
  7. #    by pvh
  8. #    of <Apple Macintosh Developer Technical Support - or wheverever>
  9. #
  10. #    Modification History
  11. #    5/27/94  rrk     Made PPC compatible
  12. #    9/21/92  rrk    Got rid of "^2 ^3" string left in dialog at init time
  13. #                    if DMZ started on system with no zones.  Put in paramText
  14. #                    call in tellUserNoZones()
  15. #
  16. #    Copyright © 1990 Apple Computer, Inc.
  17. #    All rights reserved.
  18. #    
  19. #-------------------------------------------------------------------------------------------
  20. */
  21.  
  22. #include    "dmz.h"
  23.  
  24. /* 
  25.  *    External globals we use in this dude.
  26.  */
  27. extern     DialogPtr         gMyDialog;
  28. extern     long            gSortMode;
  29. extern  EventRecord        gMyEvent;
  30. extern  short            gATalkTransFlag;
  31. extern  myATQEntry        gATQEntry;
  32. /* 
  33.  *    Globals we use here
  34.  */
  35. ListHandle                 gZonesList, gObjectTypeList;
  36. char                     gNameGlob[33];
  37.  
  38.  
  39. /* 
  40.  *    This is a simple routine that invalidates the passed in dialog item's rectangle 
  41.  *    so that we get a cleaner refresh of only the items that need updating.
  42.  */
  43. void invalidateItem(short whichItem)
  44. {
  45.     Rect         r;
  46.     short         kind;
  47.     Handle         h;
  48.     GrafPtr     savedPort;
  49.     
  50.     GetPort(&savedPort);
  51.     SetPort(gMyDialog);
  52.     GetDItem(gMyDialog, whichItem, &kind, &h, &r);
  53.     InvalRect(&r);
  54.     BeginUpdate(gMyDialog);
  55.     UpdtDialog(gMyDialog, gMyDialog->visRgn);
  56.     EndUpdate(gMyDialog);
  57.     SetPort(savedPort);
  58. }
  59.     
  60.     
  61. void DisposOfMyLists()
  62. {
  63.     LDispose(gZonesList);
  64.     LDispose(gObjectTypeList);
  65. }
  66.     
  67. void OpenZonesList()
  68. {
  69.     Rect         dBounds, rView;
  70.     Point         cSize;
  71.     GrafPtr     tp;
  72.     short        kind;
  73.     Handle        h;
  74.     
  75.     GetPort(&tp);
  76.     SetPort(gMyDialog);
  77.     GetDItem(gMyDialog, 1, &kind, &h, &rView);
  78.  
  79.     rView.top += 1;
  80.     rView.bottom -= 1;
  81.     rView.right -= 16;
  82.  
  83.     cSize.v = 0;
  84.     cSize.h = 0;
  85.  
  86.     SetRect(&dBounds, 0, 0, 1, 0); 
  87.     gZonesList = LNew(&rView, &dBounds, cSize, 0, gMyDialog, true, false, false, true);
  88.     (**gZonesList).selFlags = lUseSense+lOnlyOne;
  89.     SetPort(tp);
  90. }
  91.  
  92. void tellUserNoZones()
  93. {
  94.     Cell             theCell;
  95.     short         ignore, i;
  96.     GrafPtr     tp;
  97.     
  98.     ClearZoneCells();        // quick way to clear out an old entry - sort of overkill
  99.     GetPort(&tp);
  100.     SetPort(gMyDialog);
  101.  
  102.     LDoDraw(false, gZonesList);
  103.  
  104.     i = 0;
  105.  
  106.     theCell.v = i;
  107.     theCell.h = 0;
  108.  
  109.     ignore = LAddRow(1, i, gZonesList);    
  110.  
  111.     LClrCell(theCell, gZonesList);
  112.     if (((dmzEntryPtr) *(gATQEntry.globs))->atalkActive)  // If ATalk active.
  113.     {
  114.         LSetCell("No zones <*>.", PStrLen("\pNo zones <*>."), theCell, gZonesList);    
  115.         ParamText("\p", "\p", "\p", "\p");
  116.     }
  117.     else
  118.     {
  119.         LSetCell("AppleTalk is off.", PStrLen("\pAppleTalk is off."), theCell, gZonesList);    
  120.         ParamText("\p", "\p", "\p", "\p");
  121.     }    
  122.  
  123.     LDoDraw(true, gZonesList);
  124.     InvalRect(&(**gZonesList).rView);
  125.     SetPort(tp);
  126. }
  127.  
  128. /*****************************************************************************/
  129.  
  130.  
  131.  
  132. /* Compare two pascal-strings. */
  133.  
  134. #pragma segment StringUtils
  135. short    pcmp(StringPtr s1, StringPtr s2)
  136. {
  137.     short    i, len;
  138.     char    j;
  139.  
  140.     if ((len = s1[0]) > s2[0]) len = s2[0];
  141.  
  142.     for (i = 1; i <= len; ++i)
  143.         if (j = s1[i] - s2[i]) return(j);
  144.  
  145.     return(s1[0] - s2[0]);
  146. }
  147.     
  148.  
  149. /* 
  150.  *    compare routine for qsort().  it uses a global selector for the different 'fields' inthe list
  151.  *    they are actually offsets to the data in each cell.
  152.  *    OK...so it's really gross.
  153.  */
  154. int myCompare(char *aStr, char *bStr)
  155. {
  156.     long    sortPtr;
  157.     
  158.     switch(gSortMode) {
  159.         case sortOnZoneName:
  160.              sortPtr = myNetworkEntityObject;
  161.              break;
  162.         case sortOnType:
  163.              sortPtr = myNetworkEntityType;
  164.              break;
  165.         case sortOnNet:
  166.              sortPtr = myNetworkEntityNet;
  167.              break;
  168.         case sortOnNode:
  169.              sortPtr = myNetworkEntityNode;
  170.              break;
  171.         case sortOnSocket:
  172.              sortPtr = myNetworkEntitySocket;
  173.              break;
  174.         }
  175.     return(IUCompString((void *)(aStr + sortPtr), (void *)(bStr + sortPtr)));
  176. }
  177.  
  178. /* 
  179.   *    call qsort() 
  180.   */
  181. void letsSort(Ptr theBuffPtr, short numZonesGot, short number)
  182. {        
  183.     SetPort(gMyDialog);
  184.     
  185.     /* tell them we're sorting */
  186.     ParamText("\p", "\p", "\p", "\psorting...");        
  187.     invalidateItem(kProgressID);
  188.         
  189.     /* do the sort */
  190.     qsort(theBuffPtr, (unsigned long)numZonesGot, (unsigned long)number, myCompare);
  191.     
  192.     /* erase the info text */
  193.     ParamText("\p", "\p", "\p", "\p");        
  194.     invalidateItem(kProgressID);
  195. }
  196.  
  197.  
  198. /* 
  199.  *    set up the zone list 
  200.  */
  201. void SetZoneCells(Ptr bufferPtr, short NumZonesGot)
  202. {
  203.     Cell         theCell;
  204.     short         ignore, i;
  205.     long         bufferIndex;
  206.     GrafPtr     tp;
  207.     Str32        myZone, zoneString;
  208.         
  209. #ifndef powerc
  210.     MYVBLSPININSTALL();
  211. #endif
  212.  
  213.     myZone[0] = 0;
  214.     getMyZone((char *)myZone);
  215.     
  216.     GetPort(&tp);
  217.     SetPort(gMyDialog);
  218.  
  219.     /* added 5/5/89 pvh */
  220.     gSortMode = sortOnZoneName;
  221.     letsSort(bufferPtr, NumZonesGot, zoneNameSize);
  222.     
  223.     LDoDraw(false, gZonesList);
  224.     
  225.     bufferIndex = 0L;
  226.     i = 0;
  227.     while(i<NumZonesGot) {
  228.         ignore = LAddRow(1, i, gZonesList);    
  229.         theCell.v = i;
  230.         theCell.h = 0;
  231.                 
  232.         zoneString[0] = (char)(bufferPtr+bufferIndex)[0];
  233.         LSetCell((Ptr)bufferPtr+bufferIndex + 1L, zoneString[0], theCell, gZonesList);    
  234.  
  235.         /*
  236.          *    when we find our zone, select it in the list
  237.          */
  238.         zoneString[0] = (char)(bufferPtr+bufferIndex)[0];
  239.         BlockMove((Ptr)bufferPtr+bufferIndex + 1L, (Ptr)&zoneString + 1L, zoneString[0]);
  240.         if(EqualString(zoneString, myZone, false, false)) {
  241.             LSetSelect(true, theCell, gZonesList);
  242.             LAutoScroll(gZonesList);
  243.             getTypesNamesInZone((char *)zoneString);
  244.             }
  245.             
  246.         i += 1;
  247.         bufferIndex += zoneNameSize;
  248.         }
  249.  
  250.     LDoDraw(true, gZonesList);
  251.  
  252.     SetPort(tp);
  253.  
  254. #ifndef powerc
  255.     STOPANDREMOVESPINNINGCURSOR();
  256. #endif
  257. }
  258.  
  259. /* 
  260.  *    clear the zone list 
  261.  */
  262. void ClearZoneCells()
  263. {
  264.     GrafPtr     tp;
  265.     Str32        tempStr;
  266.  
  267. #ifndef powerc    
  268.     MYVBLSPININSTALL();
  269. #endif
  270.  
  271.     GetPort(&tp);
  272.     SetPort(gMyDialog);
  273.     
  274.     ParamText("\p", "\p", "\p", "\p");    // clear the number of items text which appears in item 7
  275.  
  276.             // clear out the "number of found items" dialog item
  277.     tempStr[0] = 0;        
  278.     setItemString(gMyDialog, (short) kObjectCountID, tempStr);
  279.  
  280.     LDoDraw(false, gZonesList);
  281.     
  282.     LDelRow(0,0, gZonesList);
  283.     EraseRect(&(**gZonesList).rView);
  284.  
  285.     LDoDraw(true, gZonesList);
  286.     InvalRect(&(**gZonesList).rView);
  287.  
  288.     SetPort(tp);
  289.  
  290. #ifndef powerc    
  291.     STOPANDREMOVESPINNINGCURSOR();
  292. #endif
  293. }
  294.  
  295. /* 
  296.  *    handle click in the zone list box 
  297.  */
  298. void doZonesListStuff()
  299. {
  300.     Boolean     tempBool;
  301.     Cell         thisCell;
  302.     Rect         scrollRect;
  303.     GrafPtr     tp;
  304.     Str255          dataPtr;
  305.     short         dataLen;
  306.     Point        localPoint = gMyEvent.where;
  307.     
  308.     thisCell.v = 0;
  309.     thisCell.h = 0;
  310.     GetPort(&tp);
  311.     SetPort(gMyDialog);
  312.     
  313.     if(gMyDialog != FrontWindow())
  314.         SelectWindow(gMyDialog);
  315.     else 
  316.     {
  317.         if (!((dmzEntryPtr) *(gATQEntry.globs))->atalkActive)  // ATalk not active.
  318.             return;
  319.         GlobalToLocal(&localPoint);
  320.         scrollRect = (**gZonesList).rView;
  321.         scrollRect.left = (**gZonesList).rView.right;
  322.         scrollRect.right = scrollRect.left + 15;
  323.         if(PtInRect(localPoint, &scrollRect))
  324.             tempBool = LClick(localPoint, gMyEvent.modifiers, gZonesList);
  325.         else if(PtInRect(localPoint, &(**gZonesList).rView)) {
  326.             if(LClick(localPoint, gMyEvent.modifiers, gZonesList)) {    
  327.                 LGetSelect(true, &thisCell, gZonesList);
  328.                 dataLen = 255;
  329.                 LGetCell(&dataPtr[1], &dataLen, thisCell, gZonesList);
  330.                 dataPtr[0] = dataLen;
  331.                 BlockMove(&dataPtr, &gNameGlob[0], 34L);
  332.                 getTypesNamesInZone((char *)dataPtr);
  333.                 }
  334.             else {
  335.                 LGetSelect(true, &thisCell, gZonesList);
  336.                 dataLen = 255;
  337.                 LGetCell(&dataPtr[1], &dataLen, thisCell, gZonesList);
  338.  
  339.                 dataPtr[0] = dataLen;
  340.  
  341.                 BlockMove(&dataPtr[0], &gNameGlob[0], 34L);
  342.                 }
  343.             }
  344.         }
  345.     SetPort(tp);
  346. }
  347.  
  348. void OpenObjectTypeList()
  349. {
  350.     Rect         dBounds, rView;
  351.     Point         cSize;
  352.     GrafPtr     tp;
  353.     Handle        h;
  354.     short        kind, ignore;
  355.     
  356.     GetPort(&tp);
  357.     SetPort(gMyDialog);
  358.     GetDItem(gMyDialog, 2, &kind, &h, &rView);
  359.  
  360.     rView.top += 1;
  361.     /*
  362.     // rView.bottom -= 1;
  363.     // rView.bottom = gMyDialog->portRect.bottom-16;
  364.     */
  365.     
  366.     rView.right -= 16;
  367.  
  368.     cSize.v = 0;
  369.     cSize.h = 0;
  370.  
  371.     SetRect(&dBounds, 0, 0, 1, 0); /* two cells across by 0 down extra cell for hiding entity net address */
  372.     gObjectTypeList = LNew(&rView, &dBounds, cSize, 128, gMyDialog, true, false, false, true);
  373.     (**gObjectTypeList).selFlags = lUseSense+lOnlyOne;
  374.     
  375.     SizeControl((**gObjectTypeList).vScroll, (**(**gObjectTypeList).vScroll).contrlRect.right - 
  376.         (**(**gObjectTypeList).vScroll).contrlRect.left, (**(**gObjectTypeList).vScroll).contrlRect.bottom - 
  377.         (**(**gObjectTypeList).vScroll).contrlRect.top - 16);
  378.     
  379.     ignore = LAddColumn(1, 0, gObjectTypeList);    
  380.  
  381.     SetPort(tp);
  382. }
  383.  
  384. void doObjectDoubleClick()
  385. {
  386.     Cell                 thisCell;
  387.     short                dataLen;
  388.     long                temp;
  389.     myNetworkEntity        myEnt;
  390.     AddrBlock            address;
  391.  
  392.     thisCell.v = 0;
  393.     thisCell.h = 0;
  394.     LGetSelect(true, &thisCell, gObjectTypeList);
  395.     
  396.     /* 
  397.      *    at this point do whatever you want with the object selection.
  398.      *    send 'em lots of packets and try and kill them or whatever...
  399.      */
  400.     dataLen = sizeof(myNetworkEntity);
  401.     LGetCell((Ptr) &myEnt, &dataLen, thisCell, gObjectTypeList);
  402.  
  403.     StringToNum((void *) myEnt.net, (long *) &temp);
  404.     address.aNet = (short) temp;
  405.     StringToNum((void *) myEnt.node, (long *) &temp);
  406.     address.aNode = (short) temp;
  407.     StringToNum((void *) myEnt.socket, (long *) &temp);
  408.     address.aSocket = (short) temp;
  409.     doEcho(&myEnt);
  410. }
  411.  
  412.  
  413. void doObjectTypeListStuff()
  414. {
  415.     Boolean             ignore;
  416.     Cell                 thisCell;
  417.     Rect                 scrollRect;
  418.     GrafPtr             tp;
  419.     Point                localPoint = gMyEvent.where;
  420.     
  421.     thisCell.v = 0;
  422.     thisCell.h = 0;
  423.     GetPort(&tp);
  424.     SetPort(gMyDialog);
  425.     
  426.     if(gMyDialog != FrontWindow())
  427.         SelectWindow(gMyDialog);
  428.     else {
  429.         if (!((dmzEntryPtr) *(gATQEntry.globs))->atalkActive)  // ATalk not active.
  430.             return;
  431.         GlobalToLocal(&localPoint);
  432.         scrollRect = (**gObjectTypeList).rView;
  433.         scrollRect.left = (**gObjectTypeList).rView.right;
  434.         scrollRect.right = scrollRect.left + 15;
  435.         if(PtInRect(localPoint, &scrollRect))
  436.             ignore = LClick(localPoint, gMyEvent.modifiers, gObjectTypeList);
  437.         else if(PtInRect(localPoint, &(**gObjectTypeList).rView)) {
  438.             /*
  439.              *    Check for a double click.
  440.              */
  441.              if(LClick(localPoint, gMyEvent.modifiers, gObjectTypeList)) {
  442.                 doObjectDoubleClick();
  443.                 }
  444.             }
  445.         }
  446.     SetPort(tp);
  447. }
  448.  
  449. /* 
  450.  *    clear the object and types list 
  451.  */
  452. void ClearObjectTypesList()
  453. {
  454.     GrafPtr     tp;
  455.     
  456. #ifndef powerc    
  457.     MYVBLSPININSTALL();
  458. #endif
  459.  
  460.     GetPort(&tp);
  461.     SetPort(gMyDialog);
  462.  
  463.     LDoDraw(false, gObjectTypeList);
  464.     
  465.     LDelRow(0,0, gObjectTypeList);
  466.     EraseRect(&(**gObjectTypeList).rView);
  467.  
  468.     LDoDraw(true, gObjectTypeList);
  469.     InvalRect(&(**gObjectTypeList).rView);
  470.  
  471.     SetPort(tp);
  472.  
  473. #ifndef powerc
  474.     STOPANDREMOVESPINNINGCURSOR();
  475. #endif
  476. }
  477.  
  478. /* 
  479.  *    this routine pads strings out to various lengths.  justification to right or left can 
  480.  *    be specified.  we need this for a cleaner display as well as insuring a correct sort
  481.  *    on numeric data (i.e. the strings '1234' and ' 1234' are not equivalant, but if
  482.  *    we justify to the right, we'll always be OK.  (whoops maybe not in the case of the Japanese
  483.  *    alphabet...damn...
  484.  */
  485. void padEntry(unsigned char *entry, short length, short just)
  486. {
  487.     unsigned char     index, i;
  488.     
  489.     switch(just) {
  490.         case leftJust:
  491.             index = entry[0];    /* get length byte */
  492.             index += 1;            /* increment by 1 */
  493.         
  494.             while(index <= length) {
  495.                 entry[index] = ' ';
  496.                 index += 1;
  497.                 }    
  498.             break;
  499.         case rightJust:
  500.             index = entry[0];    /* get length byte */
  501.         
  502.             if(index<length) {
  503.                 BlockMove(&entry[1], (Ptr)&entry[1]+length-index, (long)index);
  504.                 
  505.                 i = 1;
  506.                 while(index < length) {
  507.                     entry[i] = ' ';
  508.                     i += 1;
  509.                     index += 1;
  510.                     }    
  511.                 }
  512.             break;
  513.         }
  514.     entry[0] = length;
  515. }
  516.     
  517. /* 
  518.  *    this is called when the NBPLookup has finished.  the buffer and the number of obects found
  519.  *    is added to the list.
  520.  */
  521. void SetObjectTypeCells(Ptr bufferPtr, short numDevicesGot)
  522. {
  523.     Cell                         theCell;
  524.     short                     ignore;
  525.     short                 numDevicesIndex;
  526.     GrafPtr             tp;
  527.     AddrBlock             address;
  528.     EntityName            abEntity;
  529.     unsigned char        charHolder[6];
  530.     long                g;
  531.     myNetworkEntity     myNetEnt;
  532.     Ptr                    newBuffer;
  533.     char                tempStr[10];
  534.     
  535.     /* tell the user this may take a little while... */
  536.     /*waitAWhile = GetCursor(watchCursor);
  537.     SetCursor(*waitAWhile);*/
  538.  
  539. #ifndef powerc
  540.     MYVBLSPININSTALL();
  541. #endif
  542.     
  543.     GetPort(&tp);
  544.     SetPort(gMyDialog);
  545.  
  546.     /* make room for as many objects as we need */
  547.     newBuffer = NewPtr(numDevicesGot*sizeof(myNetworkEntity));
  548.     if(newBuffer == 0L)
  549.         return;
  550.         
  551.     LDelRow(0, 0, gObjectTypeList);    /* deletes lists's cells */
  552.     
  553.     LDoDraw(false, gObjectTypeList);  /* turn drawing off */
  554.     
  555.     numDevicesIndex = 0;
  556.     
  557.     while(numDevicesIndex<numDevicesGot) {
  558.         theCell.v = numDevicesIndex;
  559.         
  560.         ignore = LAddRow(1, numDevicesIndex, gObjectTypeList);    
  561.  
  562.         NBPExtract(bufferPtr, numDevicesGot, numDevicesIndex+1, &abEntity, &address);
  563.  
  564.         /* first move address data into "hidden" cell */
  565.         theCell.h = 1;
  566.         LSetCell((Ptr) &address, 4, theCell, gObjectTypeList);
  567.         
  568.         /* now move object name & type into one cell */
  569.         theCell.h = 0;
  570.         
  571.         /* object */
  572.         BlockMove(&abEntity.objStr, &myNetEnt.object, 33L);    
  573.         if(myNetEnt.object[0] > 32)
  574.             myNetEnt.object[0] = 32;
  575.             
  576.         /* type */
  577.         BlockMove(&abEntity.typeStr, &myNetEnt.type, 33L);
  578.         if(myNetEnt.type[0] > 32)
  579.             myNetEnt.type[0] = 32;
  580.         
  581.         /* network */
  582.         g = (long)address.aNet;
  583.         g = g & 0x0000FFFF; /* mask out hiword crap */
  584.         NumToString(g, (void *) &charHolder);
  585.         padEntry((void *) &charHolder, 5, rightJust);
  586.         BlockMove(&charHolder, &myNetEnt.net, 6L);
  587.  
  588.         /* node */
  589.         NumToString((long)address.aNode, (void *) &charHolder);
  590.         padEntry((void *) &charHolder, 3, rightJust);
  591.         BlockMove(&charHolder, &myNetEnt.node, 4L);
  592.     
  593.         /* socket */
  594.         NumToString((long)address.aSocket, (void *) &charHolder);
  595.         padEntry((void *) &charHolder, 3, rightJust);
  596.         BlockMove(&charHolder, &myNetEnt.socket, 4L);
  597.                 
  598.         BlockMove((Ptr) &myNetEnt, (Ptr)newBuffer+(numDevicesIndex)*sizeof(myNetworkEntity), 
  599.             sizeof(myNetworkEntity));
  600.  
  601.         LSetCell((Ptr)&myNetEnt, sizeof(myNetworkEntity), theCell, gObjectTypeList);    
  602.  
  603.         numDevicesIndex += 1; 
  604.         }
  605.  
  606.     /* do a quicksort() on the mess */
  607.     letsSort(newBuffer, numDevicesGot, sizeof(myNetworkEntity));
  608.     
  609.     numDevicesIndex = 0;
  610.     
  611.     while(numDevicesIndex<numDevicesGot) {
  612.         theCell.v = numDevicesIndex;
  613.         theCell.h = 0;
  614.         BlockMove((Ptr)newBuffer+(numDevicesIndex)*sizeof(myNetworkEntity), (Ptr) &myNetEnt, sizeof(myNetworkEntity));
  615.         LSetCell((Ptr)&myNetEnt, sizeof(myNetworkEntity), theCell, gObjectTypeList);    
  616.         numDevicesIndex += 1;
  617.         }
  618.         
  619.     DisposPtr(newBuffer);
  620.     
  621.     NumToString(numDevicesIndex, (void *) tempStr);
  622.     ParamText((ConstStr255Param)tempStr, "\p# of objects: ", "\p", "\p");
  623.     
  624.     LDoDraw(true, gObjectTypeList);
  625.     invalidateItem(8);
  626.     invalidateItem(2);
  627.  
  628.     SetPort(tp);
  629. #ifndef powerc
  630.     STOPANDREMOVESPINNINGCURSOR();
  631. #endif
  632.     InitCursor();
  633. }
  634.  
  635.  
  636.